package com.fanfanwang.ffw.app.ui.service;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.ServiceNewListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetNewsListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.NewsListBean;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 新闻资讯列表
 * 
 * @author Administrator
 * 
 */
public class ServiceNewListAct extends BaseActivity {
	private ServiceNewListAct mAct;
	private CommonTitleBar commonTitleBar;
	// public PersonalMoneyListAdapter listAdapter;
	// private ArrayList<PersonalMoneyBean> personalMoneyBeans;
	public static PullToRefreshListView mPullToRefreshListView;
	private ListView listView;
	private ServiceNewListAdapter adapter;
	public int page = 1;
	public String pagesize = "20";
	public static IUpdateServiceNew iUpdateServiceNew;
	private ArrayList<NewsListBean> lists;
	/**
	 * 网络错误布局layout
	 */
	private LinearLayout llNetError;
	/**
	 * 网络错误显示图片
	 */
	private ImageView ivNetErrorReload;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}

	public void setCallback() {
		iUpdateServiceNew = new IUpdateServiceNew() {
			@Override
			public void setServicNewList(ArrayList<NewsListBean> lists) {
				// 判断一下这里的是什么意思，这个是得到的是就是这一页的还是这些页之前的所有的。

				adapter.addList(lists);
				adapter.notifyDataSetChanged();
				page++;
			}
		};
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.car_title));
		commonTitleBar.setReturnVisible();
		if (AppContext.instance.isNetworkConnected()) {
			// 网络可用加载数据
			llNetError.setVisibility(View.GONE);
			
		} else {
			// 网络不可用显示网络错误页面
			llNetError.setVisibility(View.VISIBLE);
		}
		// mCommonTitleBar.setTv_return_title(getString(R.string.person_money_zhangdan));
		// mListView.setAdapter(listAdapter);
		mPullToRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						new GetNewsListAsyncTask(mAct, page + "", pagesize)
								.execute("");
					}
				});
		setCallback();
		listView.setAdapter(adapter);
		new GetNewsListAsyncTask(mAct, page + "", pagesize).execute("");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// MyObserver.unregiserOberverBitmap();
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_service_new);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.lv_new);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		listView = mPullToRefreshListView.getRefreshableView();
		lists = new ArrayList<NewsListBean>();
		adapter = new ServiceNewListAdapter(mAct, lists);
		llNetError = (LinearLayout) findViewById(R.id.llNetError);
		ivNetErrorReload = (ImageView) findViewById(R.id.ivNetErrorReload);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		ivNetErrorReload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (AppContext.instance.isNetworkConnected()) {
					// 网络可用加载数据
					llNetError.setVisibility(View.GONE);
					new GetNewsListAsyncTask(mAct, page + "", pagesize).execute("");
				} else {
					// 网络不可用显示网络错误页面
					llNetError.setVisibility(View.VISIBLE);
				}
			}
		});

	}

	public interface IUpdateServiceNew {
		public void setServicNewList(ArrayList<NewsListBean> lists);
	}
}
